package com.metro.activation.common.utils;

import java.lang.reflect.Method;
import java.util.List;

import com.metro.activation.common.beans.DicTypeMapper;
import com.metro.activation.dic.mapper.IDicMapper;
import com.metro.core.vo.LabelValue;
import com.metro.activation.common.utils.resource.Res;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.Cache;

/**
 * 数据字典处理帮助类
 * @author xk
 * @since [1.0.1]
 * @version [1.0.1, 2016年10月10日]
 */
public class DicUtil {

    public static Logger logger = LoggerFactory.getLogger(DicUtil.class);
    /**
     * 以键值对的形式返回同一类型的键值对,优先从缓存中取，如果缓存中没有就从数据库中加载到缓存中。
     * @return
     * @author xk
     */
    public static List<? extends LabelValue> getLabelValueList(DicTypeMapper dicTypeMapper) {
        return getLabelValueList(dicTypeMapper, null, null);
    }

    /**
     * 带参数的查询,以键值对的形式返回同一类型的键值对,优先从缓存中取，如果缓存中没有就从数据库中加载到缓存中
     * @param dicTypeMapper 字典类型与mapper映射关系
     * @param keyExt 缓存时key的扩展部分，因为是一个分类下的多个列表，所以要用此部分加以区分
     * @param param 参数，一个实体对象
     * @return
     */
    public static <T> List<? extends LabelValue> getLabelValueList(DicTypeMapper dicTypeMapper, String keyExt, T param) {
        Cache dict = Res.getCache("dict");
        String key = dicTypeMapper.getType();
        if(keyExt != null){
            key += keyExt;
        }
        if(dict != null){
            List<LabelValue> dicDataList = dict.get(key, List.class);
            if(dicDataList != null){
                return dicDataList;
            }
        }

        if(dicTypeMapper.getMethodName() != null){
            Object dicMapper = Res.getApplicationContext().getBean(dicTypeMapper.getMapperClass());
            try {
                Method method = null;
                List list = null;
                if(param == null){
                    method = dicTypeMapper.getMapperClass().getMethod(dicTypeMapper.getMethodName());
                    list = (List)method.invoke(dicMapper);
                }else{
                    method = dicTypeMapper.getMapperClass().getMethod(dicTypeMapper.getMethodName(), param.getClass());
                    list = (List)method.invoke(dicMapper, param);
                }
                if(list != null && list.size() > 0){
                    if(dict != null){
                        dict.put(key, list);
                    }
                    return list;
                }
            } catch (Exception e) {
                logger.error("[dic]方法调用出错：", e);
            }
        }else{
            IDicMapper dicMapper = Res.getApplicationContext().getBean(IDicMapper.class);
            List list = dicMapper.queryDicDataByRootCode(dicTypeMapper.getType());
            if(list != null && list.size() > 0){
                if(dict != null){
                    dict.put(key, list);
                }
                return list;
            }
        }


        return null;
    }
    
    /**
     * 获取某一类型下id所对应的name
     * 优先从缓存中获取
     * @author xk
     */
    public static String getName(DicTypeMapper dicTypeMapper, Object objId) {
        if(dicTypeMapper != null && objId != null){
            String id = objId.toString();
            List<? extends LabelValue> dataList = getLabelValueList(dicTypeMapper);
            if(dataList != null && dataList.size() > 0){
                for (LabelValue labelValue : dataList) {
                    if(labelValue.getValue().equals(id)){
                        return labelValue.getLabel();
                    }
                }
            }
        }
        return null;
    }

    /**
     * 获取某一类型下name所对应的id 字符串
     * 优先从缓存中获取
     * @author xk
     */
    public static String getIdStr(DicTypeMapper dicTypeMapper, String name) {
        if(dicTypeMapper != null && name != null){
            List<? extends LabelValue> dataList = getLabelValueList(dicTypeMapper);
            if(dataList != null && dataList.size() > 0){
                for (LabelValue labelValue : dataList) {
                    if(labelValue.getLabel().equals(name)){
                        return labelValue.getValue();
                    }
                }
            }
        }
        return null;
    }

    /**
     * 获取某一类型下name所对应的id Long
     * 优先从缓存中获取
     * @author xk
     */
    public static Long getId(DicTypeMapper dicTypeMapper, String name) {
        String idStr = getIdStr(dicTypeMapper, name);
        if(idStr != null){
            return Long.valueOf(idStr);
        }
        return null;
    }

    /**
     * 从缓存中清除相关数据字典的数据
     * @param key
     */
    public static void removeKeyFromCache(String key){
        Cache dict = Res.getCache("dict");
        if(dict != null){
            dict.evict(key);
        }
    }

    /**
     * 从缓存中清除相关数据字典的数据
     * @param type
     */
    public static void removeKeyFromCache(DicTypeMapper type){
        Cache dict = Res.getCache("dict");
        if(dict != null){
            dict.evict(type.getType());
        }
    }

}
